﻿using Wise.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Wise.Util;

namespace MyWpf.Controls
{
    public partial class UserGuider : Form
    {
        public UserGuider()
        {
            InitializeComponent();
        }

        private void btnOpenConfig_Click(object sender, EventArgs e)
        {
            if (openConfigFile.ShowDialog() == DialogResult.OK)
            {
                string fileName = openConfigFile.FileName;
                MyDbConfig.Current = new MyDbConfig(fileName).Read();
                btnRunPage.Enabled = true;
                for (int i = 1; i < tabs.TabCount; i++)
                {
                    tabs.TabPages[i].Show();
                }
            }
        }
        private void btnOpenExcel_Click(object sender, EventArgs e)
        {
            if (openExcelFile.ShowDialog() == DialogResult.OK)
            {
                string fileName = openExcelFile.FileName;
                if (MyDbConfig.Current == null)
                {
                    MyDbConfig.Current = new MyDbConfig(fileName);
                }
                //MyDbConfig.Current.
            }
        }

        private void btnRunPage_Click(object sender, EventArgs e)
        {
            tabPage5.Show();
        }

        #region 页面切换

        private void btnNext1_Click(object sender, EventArgs e)
        {
            MyDbConfig.Current = new MyDbConfig();
            tabs.SelectedIndex = 1;

        }

        private void btnPreview0_Click(object sender, EventArgs e)
        {
            tabs.SelectedIndex = 0;
        }

        private void btnNext2_Click(object sender, EventArgs e)
        {
            tabs.SelectedIndex = 2;
        }

        private void btnPreview1_Click(object sender, EventArgs e)
        {
            tabs.SelectedIndex = 1;
        }

        private void btnNext3_Click(object sender, EventArgs e)
        {
            tabs.SelectedIndex =3;
        }

        private void btnPreview2_Click(object sender, EventArgs e)
        {
            tabs.SelectedIndex = 2;
        }

        private void btnNext4_Click(object sender, EventArgs e)
        {
            tabs.SelectedIndex =4;
        }

        private void btnPreview3_Click(object sender, EventArgs e)
        {
            tabs.SelectedIndex =3;
        }

        private void btnNext5_Click(object sender, EventArgs e)
        {
            tabs.SelectedIndex = 5;
        }

        #endregion

        private void butSaveConfig_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(MyDbConfig.Current.GetConfigFileFullName()))
            {
                if (saveConfigFile.ShowDialog() == DialogResult.OK)
                {
                    var cur = MyDbConfig.Current;
                    var newCfg = new MyDbConfig(saveConfigFile.FileName);
                    cur.CopyTo(newCfg);
                    MyDbConfig.Current = newCfg;
                }
                else
                {
                    return;
                }
            }
            MyDbConfig.Current.SubmitChanges();
        }


        #region 页面激活处理
        bool[] _INIT_STATUS = new bool[6];
        //List<TabPage> pages = new List<TabPage>();

        private void tabPage0_Enter(object sender, EventArgs e)
        {
            //pages.AddRange(tabs.TabPages.Cast<TabPage>());
            //while(tabs.TabPages.Count>1)
            //{
            //    tabs.TabPages.RemoveAt(1);
            //}
        }

        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabPage1_Enter(object sender, EventArgs e)
        {
            if (!_INIT_STATUS[1])
            {
                _INIT_STATUS[1] = true;
                if (MyDbConfig.Current.数据源.使用Windows验证)
                {
                    radioSourceWin.Checked = true;
                }
            }
        }

        private void tabPage2_Enter(object sender, EventArgs e)
        {

        }

        private void tabPage3_Enter(object sender, EventArgs e)
        {

        }
        #endregion

        private void tabs_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPageIndex >= 1)
            {
                if (MyDbConfig.Current == null)
                {
                    e.Cancel = true;
                }
            }
        }

        private void drpSourceServer_TextChanged(object sender, EventArgs e)
        {
            MyDbConfig.Current.数据源.服务器名称 = drpSourceServer.Text;
        }

        private void radioSource_CheckedChanged(object sender, EventArgs e)
        {
            if(radioSourceSA.Checked)
            {
                pnlSource.Enabled = true;
            }
            else
            {
                pnlSource.Enabled = false;
            }
            MyDbConfig.Current.数据源.使用Windows验证 =! radioSourceSA.Checked;
        }

        private void radioTarget_CheckedChanged(object sender, EventArgs e)
        {
            if (radioTargetSA.Checked)
            {
                pnlTarget.Enabled = true;
            }
            else
            {
                pnlTarget.Enabled = false;
            }
            MyDbConfig.Current.目标数据库.使用Windows验证 = !radioTargetSA.Checked;
        }

        private void txtSourceUserName_TextChanged(object sender, EventArgs e)
        {
            MyDbConfig.Current.数据源.用户名 = txtSourceUserName.Text;
        }

        private void txtSourcePassword_TextChanged(object sender, EventArgs e)
        {
            MyDbConfig.Current.数据源.密码 = txtSourcePassword.Text;
        }

        private void label9_Click(object sender, EventArgs e)
        {

        }
    }
}
